package cn.wolfcode.wolf2w.controller;

import cn.wolfcode.wolf2w.annotation.RequireLogin;
import cn.wolfcode.wolf2w.annotation.UserParam;
import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.domain.StrategyComment;
import cn.wolfcode.wolf2w.domain.StrategyTheme;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.query.StrategyCommentQuery;
import cn.wolfcode.wolf2w.query.StrategyQuery;
import cn.wolfcode.wolf2w.redis.vo.StrategyStatisVO;
import cn.wolfcode.wolf2w.service.IStrategyCommentService;
import cn.wolfcode.wolf2w.service.IStrategyService;
import cn.wolfcode.wolf2w.service.IStrategyThemeService;
import cn.wolfcode.wolf2w.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/strategies")
public class StrategyController {

    @Autowired
    private IStrategyService strategyService;

    @Autowired
    private IStrategyThemeService strategyThemeService;

    @Autowired
    private IStrategyCommentService strategyCommentService;




    /**
     * 查询某个攻略
     */
    @GetMapping("/detail")
    public Object getToasts(String id) {

        Strategy strategy = strategyService.get(id);

        // 当用户每一次进入这个映射方法,表示阅读一遍,阅读量 + 1
        if (strategy != null) {
            strategyService.addViewnum(id);
        }
        return JsonResult.success(strategy);

    }

    /**
     *  查询目的地下所有攻略
     */
    @GetMapping("/query")
    public Object queryStrategies(StrategyQuery qo){
        Page page = strategyService.queryStrategiesByDestId(qo);
        return JsonResult.success(page);
    }

    /**
     * 查询所有主题
     */
    @GetMapping("/themes")
    public Object queryThemes(){
         List<StrategyTheme> themes = strategyThemeService.list();
        return JsonResult.success(themes);
    }
    /**
     * 查询所有主题
     */
    @GetMapping("/strategies/viewnumTop3")
    public Object viewnumTop3(){
         List<StrategyTheme> themes = strategyThemeService.list();
        return JsonResult.success(themes);
    }


    /**
     * 评论分页查询
     */
    @GetMapping("comments")
    public Object comments(StrategyCommentQuery qo){
        Page<StrategyComment> page = strategyCommentService.query(qo);
        return JsonResult.success(page);
    }

    /**
     * 攻略明细下添加评论
     */
    @RequireLogin       // 需要登录后才能评论
    @PostMapping("addComment")
    public Object addComment(StrategyComment comment, String strategyId,@UserParam UserInfo userInfo){
        strategyCommentService.addComment(comment,strategyId,userInfo);
        // 添加一条评论,评论数 + 1
        strategyService.addReplynum(strategyId);
        return JsonResult.success();
    }

    /**
     * 点赞操作
     * @param cid   评论id
     * @param sid   攻略明细的id
     */
    @RequireLogin
    @PostMapping("commentThumb")
    public Object commentThumb(String cid,String sid,@UserParam UserInfo userInfo){
        // 获取当前登录用户的id
        String uid = userInfo.getId();
        strategyCommentService.addThumb(cid,sid,uid);
        return JsonResult.success();
    }

    /**
     * 获取vo对象
     * @param sid
     * @return
     */
    @GetMapping("statisVo")
    public Object statisVo(String sid){
        StrategyStatisVO vo = strategyService.getStatisVo(sid);
        return JsonResult.success(vo);
    }

    /**
     * 收藏/取消收藏操作
     */
    @RequireLogin
    @PostMapping("favor")
    public Object favor(String sid,@UserParam UserInfo userInfo){
        return JsonResult.success(strategyService.favor(sid,userInfo));
    }

    /**
     * 攻略点赞(顶)
     */
    @RequireLogin
    @PostMapping("strategyThumbup")
    public Object strategyThumbup(String sid,@UserParam UserInfo userInfo){
        boolean thumb = strategyService.strategyThumbup(sid,userInfo);
        return JsonResult.success(thumb);
    }
}
